﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLVatTu.Class;

namespace QLVatTu.HeThong
{
    public partial class frmPhanQuyen : Form
    {
        public frmPhanQuyen()
        {
            InitializeComponent();
        }

        KetNoiDB cn = new KetNoiDB();

        private void load_data()
        {
            DataTable dtQuyenHan = cn.getTable("SELECT * FROM tbl_QuyenHan");
            DataRow drQuyenHan = dtQuyenHan.NewRow();
            drQuyenHan["MaQuyenHan"] = "-1";
            drQuyenHan["TenQuyenHan"] = "---Chọn nhóm thành viên---";
            dtQuyenHan.Rows.InsertAt(drQuyenHan, 0);
            cbbNhomThanhVien.DataSource = dtQuyenHan;
            cbbNhomThanhVien.DisplayMember = "TenQuyenHan";
            cbbNhomThanhVien.ValueMember = "MaQuyenHan";

            load_treeview();
        }

        private void load_treeview()
        {
            DevComponents.AdvTree.Node tNode = new DevComponents.AdvTree.Node();
            tNode.Text = "-1";
            tNode.Text = "Tất cả";
            tNode.Expanded = true;
            tNode.CheckBoxVisible = true;
            DataTable dtCapPhep = cn.getTable("SELECT MaCapPhep,TenCapPhep FROM tbl_CapPhep");
            if (dtCapPhep.Rows.Count > 0)
            {
                foreach (DataRow dr in dtCapPhep.Rows)
                {
                    DevComponents.AdvTree.Node tNodeChild = new DevComponents.AdvTree.Node();
                    tNodeChild.Name = dr["MaCapPhep"].ToString();
                    tNodeChild.Text = dr["TenCapPhep"].ToString();
                    tNodeChild.CheckBoxVisible = true;
                    tNode.Nodes.Add(tNodeChild);
                }
                advtPhanQuyen.Nodes.Add(tNode);
            }
        }

        private void frmPhanQuyen_Load(object sender, EventArgs e)
        {
            if (cn.KetNoi())
            {
                load_data();
            }
            else
            {
                this.Close();
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (cbbNhomThanhVien.Items.Count > 0 && cbbNhomThanhVien.SelectedValue != null)
            {
                if (cbbNhomThanhVien.SelectedValue.ToString() == "QH001")
                {
                    MessageBox.Show("Bạn không thể phân quyền lại nhóm quản trị hệ thống!");
                }
                else if (cbbNhomThanhVien.SelectedValue.ToString() != "-1")
                {
                    // Xóa các danh sách quyền trước
                    cn.Delete_Table("tbl_PhanQuyen", "MaQuyenHan = '" + cbbNhomThanhVien.SelectedValue.ToString() + "'");
                    // Thêm các danh sách quyền mới
                    foreach (DevComponents.AdvTree.Node node in advtPhanQuyen.Nodes)
                    {
                        if (node.Nodes.Count > 0)
                        {
                            foreach (DevComponents.AdvTree.Node childNode in node.Nodes)
                            {
                                if (childNode.Checked == true)
                                {
                                    cn.Insert_Table("tbl_PhanQuyen", "MaQuyenHan,MaCapPhep", "'" + cbbNhomThanhVien.SelectedValue.ToString() + "','" + childNode.Name + "'");
                                }
                            }
                        }
                    }
                    MessageBox.Show("Lưu thành công!");
                }
                else
                {
                    MessageBox.Show("Bạn chưa chọn nhóm thành viên để phân quyền!");
                }
            }
        }

        private void CheckAllChildNodes(DevComponents.AdvTree.Node treeNode, bool nodeChecked)
        {
            foreach (DevComponents.AdvTree.Node node in treeNode.Nodes)
            {
                node.Checked = nodeChecked;
                if (node.Nodes.Count > 0)
                {
                    this.CheckAllChildNodes(node, nodeChecked);
                }
            }
        }

        private void advtPhanQuyen_NodeClick(object sender, DevComponents.AdvTree.TreeNodeMouseEventArgs e)
        {
            if (e.Node.Nodes.Count > 0)
            {
                CheckAllChildNodes(e.Node, e.Node.Checked);
            }
        }

        private void HienThiQuyenNhomThanhVien(string _MaQuyenHan, bool _nodeEnable)
        {
            DataTable dtPhanQuyen = cn.getTable("SELECT * FROM [tbl_PhanQuyen] WHERE MaQuyenHan = '" + _MaQuyenHan + "'");
            foreach (DevComponents.AdvTree.Node node in advtPhanQuyen.Nodes)
            {
                int countCheck;
                if (node.Nodes.Count > 0)
                {
                    countCheck = 0;
                    foreach (DevComponents.AdvTree.Node childNode in node.Nodes)
                    {
                        childNode.Enabled = _nodeEnable;
                        childNode.Checked = false;
                        if (dtPhanQuyen.Rows.Count > 0)
                        {
                            foreach (DataRow dr in dtPhanQuyen.Rows)
                            {
                                if (childNode.Name == dr["MaCapPhep"].ToString())
                                {
                                    childNode.Checked = true;
                                    countCheck++;
                                }
                            }
                        }
                    }
                    node.Enabled = _nodeEnable;
                    if (countCheck == node.Nodes.Count)
                    {
                        node.Checked = true;
                    }
                    else
                    {
                        node.Checked = false;
                    }
                }
            }
        }

        private void cbbNhomThanhVien_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbNhomThanhVien.Items.Count > 0 && cbbNhomThanhVien.SelectedValue != null)
            {
                bool _nodeEnable = true;
                if (cbbNhomThanhVien.SelectedValue.ToString() == "QH001")
                {
                    _nodeEnable = false;
                    // Tự động thêm quyền cho ngưởi quản trị hệ thống nếu chưa dc cấp quyền
                    DataTable dtPhanQuyen = cn.getTable("SELECT * FROM [tbl_PhanQuyen] WHERE MaQuyenHan = '" + cbbNhomThanhVien.SelectedValue.ToString() + "'");
                    DataTable dtCapPhep = cn.getTable("SELECT * FROM tbl_CapPhep");
                    if (dtPhanQuyen.Rows.Count == 0 || dtPhanQuyen.Rows.Count < dtCapPhep.Rows.Count)
                    {
                        foreach (DataRow dr in dtCapPhep.Rows)
                        {
                            DataTable dtCheckPhanQuyen = cn.getTable("SELECT * FROM [tbl_PhanQuyen] WHERE MaQuyenHan = '" + cbbNhomThanhVien.SelectedValue.ToString() + "' AND MaCapPhep= '" + dr["MaCapPhep"].ToString() + "'");
                            if (dtCheckPhanQuyen.Rows.Count == 0)
                            {
                                cn.Insert_Table("tbl_PhanQuyen", "MaQuyenHan,MaCapPhep", "'" + cbbNhomThanhVien.SelectedValue.ToString() + "','" + dr["MaCapPhep"].ToString() + "'");
                            }
                        }
                    }
                }
                HienThiQuyenNhomThanhVien(cbbNhomThanhVien.SelectedValue.ToString(), _nodeEnable);
            }
        }

    }
}
